package juc.lock;

import juc.factory.UnsafeFactory;
import sun.misc.Unsafe;

public class CASLock {

    private volatile int state;

    private static final Unsafe UNSAFE;

    private static final long OFFSET;

    static {
        UNSAFE=UnsafeFactory.getUnsafe();
        OFFSET=UnsafeFactory.getFieldOffset(UNSAFE,CASLock.class,"state");
    }

    public boolean cas(){
        return UNSAFE.compareAndSwapInt(this,OFFSET,0,1);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}
